
const { brand_controller_save,
    brand_controller_find, brand_controller_delete,
    brand_controller_get } = require("../../../controller/product/brand/BRAND.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");

/**
 * @description 分类路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/product/brand");

/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        const {        // shopId,
            id,
            brandName = undefined,
            brandIcon = undefined,
            classId = undefined,
            sort = undefined,
            status = undefined
        } = ctx.request.body
        const check = emptyCheck({ brandName, brandIcon })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await brand_controller_save({
                id, brandName, brandIcon, classId, sort, status
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})




/**@description 根据id获取分类详情 */
router.get("/get", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await brand_controller_get({ id });
        console.log("查询：\n", id)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除分类 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await brand_controller_delete({ ids: [id] });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})


// 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, brandName, status, classId } = ctx.request.body;
        const res = await brand_controller_find({
            current, pageSize, brandName, status, classId
        })
        ctx.body = res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})



module.exports = router